package cn.zyb.controller;

import cn.zyb.domain.User;
import cn.zyb.feignclient.MovieFeignClient;
import cn.zyb.service.IUserService;
import cn.zyb.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;
    @Autowired
    private MovieFeignClient movieFeignClient;

    @GetMapping("/{id}")
    public Result findOne(@PathVariable("id")  Long id){
        User user = userService.findOne(id);
        return new Result(true,"查询成功",user);
    }

    @PostMapping
    public Result update(@RequestBody User user){
        try {
            userService.update(user);
            return new Result(true,"用户信息修改成功!",null);
        } catch (Exception e) {
            return new Result(false,"系统繁忙!!!",null);
        }
    }

    /**
     * 新增用户
     */
    @PostMapping("/add")
    public Result add(@RequestBody User user){
        try {
            //添加用户信息
            userService.add(user);
            //赠送电影票
            movieFeignClient.addOneTicket(user);
            return new Result(true,"用户信息添加成功!!!电影票赠送成功!!!快去体验吧!!!",null);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"系统繁忙!!!",null);
        }
    }
}
